import {
  EButtonSelectResult,
  IDialogX,
  IMessageDialogOptions,
  EWaitDialogXType,
} from "./types";
import { NativeModules } from "react-native";

const RNDialogX = NativeModules.RNDialogX;

class DialogX implements IDialogX {
  showLoading(content = ""): void {
    RNDialogX.showLoading(content);
  }

  dismissLoading(): void {
    RNDialogX.dismissLoading();
  }

  showTipDialog(
    content: string,
    type = EWaitDialogXType.SUCCESS,
    duration = 1500,
  ): void {
    RNDialogX.showTipDialog(content, type, duration);
  }

  showSelectDialog(
    options: IMessageDialogOptions | string,
  ): Promise<EButtonSelectResult> {
    const newOptions =
      typeof options === "string"
        ? {
            content: options,
            title: "提示",
            okText: "确定",
            cancelText: "取消",
          }
        : options;
    return new Promise(resolve => {
      RNDialogX.showMessageDialog(
        newOptions,
        (result: EButtonSelectResult) => {
          resolve(result);
        },
      );
    });
  }

  showMessageDialog(
    options: IMessageDialogOptions | string,
  ): Promise<EButtonSelectResult> {
    const newOptions =
      typeof options === "string"
        ? {
            content: options,
            title: "提示",
            okText: "确定",
          }
        : options;
    return new Promise(resolve => {
      RNDialogX.showMessageDialog(
        newOptions,
        (result: EButtonSelectResult) => {
          resolve(result);
        },
      );
    });
  }
}

export const dialogx = new DialogX();
